package com.wangxin.study8.threadtest;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @author jojo.wang
 * @title: TicketsTest
 * @projectName study8
 * @description: 经典多窗口卖票
 * @date 2019/5/716:25
 */

public class TicketsTest {
    public static void main(String[] args) throws Exception{
        ExecutorService pool = Executors.newFixedThreadPool(10);
        TicketsThread t = new TicketsThread();
        for (int i = 0; i < 5; i++) {
            Thread thread = new Thread(t,"窗口"+i);
            pool.submit(thread);
        }
        pool.shutdown();
    }
}
class TicketsThread implements Runnable{
    public static int t = 100;
    ReentrantLock lock = new ReentrantLock();
    @Override
    public void run() {
        try {
            for (;;){
                sell();
                Thread.sleep(100L);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }
    private void sell(){
        lock.lock();
        if (t>0){
            System.out.printf("%s线程正在出售第%s张票\n",Thread.currentThread().getName(),t--);
        }else {
            return;
        }
        lock.unlock();
    }
}
